package com.qqqq.web;

import com.qqqq.core.AjaxResult;
import com.qqqq.core.BaseHttpServlet;
import com.qqqq.dao.impl.StudentDaoImpl;
import com.qqqq.domain.Student;
import com.qqqq.service.StudentService;
import com.qqqq.service.impl.StudentServiceImpl;
import com.qqqq.utils.RequestUtils;
import com.qqqq.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/look")
public class SelectStuServlet extends BaseHttpServlet {
    private StudentService studentService = new StudentServiceImpl(new StudentDaoImpl());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getParameterMap().containsKey("id")) {
            ResponseUtils.responseJson(resp, AjaxResult.error(400, "请传入id"));
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        executeBusiness(resp, response -> {
            Student student = studentService.getStudent(id);
            ResponseUtils.responseJson(resp, AjaxResult.success(student));
        });
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = RequestUtils.getReqJsonBody(req, Student.class);
        int pageNum = Integer.parseInt(req.getParameter("pageNum") == null ? "1" : req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize"));
        executeBusiness(resp, response -> {
            ResponseUtils.responseJson(resp, AjaxResult.success(studentService.pageStudent(student.getStuMajorId(), student.getStuClassId(), pageNum, pageSize)));
        });
    }
}
